package com.aplink.generic.pojo;

import java.util.ArrayList;
import java.util.Locale;

import android.text.TextUtils;

import com.aplink.generic.dictionary.GenericDictionary;

public class GenericObject extends GenericDictionary<String, GenericAttribute> {

    private static final long serialVersionUID = 4050100780368437551L;

    public static boolean isContainValueOf(final GenericObject genericObject,
            final ObjPropInfo objPropInfo) {
        if (genericObject == null) {
            return false;
        }
        return genericObject.isContainValueOf(objPropInfo);
    }

    private GenericDictionary<String, ArrayList<GenericObject>> mChilds;

    private String objectMappingName;

    public GenericObject() {
        this.mChilds = new GenericDictionary<String, ArrayList<GenericObject>>();
    }

    public GenericObject(
            final GenericDictionary<String, GenericAttribute> hashMap) {
        for (int i = 0; i < hashMap.size(); i++) {
            final int key = hashMap.keyAt(i);
            this.put(key, hashMap.get(key));
        }
    }

    @Override
    public GenericObject clone() {
        final GenericObject genericObject = new GenericObject(this);
        genericObject.objectMappingName = this.objectMappingName;
        genericObject.mChilds = this.mChilds;
        return genericObject;
    }

    public boolean contains(final String key,
            final ArrayList<String> searchFields) {
        String value = null;
        for (final String searchField : searchFields) {
            value = getValue(searchField);
            if (!TextUtils.isEmpty(value)) {
                if (value.toLowerCase(Locale.getDefault()).contains(
                        key.toLowerCase(Locale.getDefault()))) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public GenericAttribute get(final String key) {
        return super.get(key);
    }

    public String getTableName() {
        return this.objectMappingName;
    }

    public String getValue(final ObjPropInfo objPropInfo) {
        if (this.get(objPropInfo.getFullColumnName()) == null) {
            return null;
        }
        return this.get(objPropInfo.getFullColumnName()).getValue();
    }

    public String getValue(final String key) {
        if (this.get(key) == null) {
            return null;
        }
        return this.get(key).getValue();
    }

    public boolean isContainValueOf(final ObjPropInfo objPropInfo) {
        if (containsKey(objPropInfo.getFullColumnName())) {
            if (this.get(objPropInfo.getFullColumnName()) != null) {
                return true;
            }
        }
        return false;
    }

    public boolean isNullValue(final String key) {
        if (this.get(key) == null) {
            return true;
        }
        if (this.get(key).getValue() == null) {
            return true;
        }
        return false;
    }

    public void merge(
            final GenericDictionary<String, GenericAttribute> genericAttributes) {
        for (int i = 0; i < genericAttributes.size(); i++) {
            final int key = genericAttributes.keyAt(i);
            final GenericAttribute attribute = genericAttributes.get(key);
            this.put(key, attribute);
        }
    }

    public void put(final String key, final String content) {
        GenericAttribute attribute = this.get(key);
        if (attribute == null) {
            attribute = new GenericAttribute(key, content);
        }
        attribute.setValue(content);
        this.remove(key);
        this.put(key, attribute);
    }

    public void setTableName(final String objectMappingName) {
        this.objectMappingName = objectMappingName;
    }
}
